package com.lzy.api.interceptors;

import com.lzy.exception.GraceException;
import com.lzy.grace.result.ResponseStatusEnum;
import com.lzy.utils.RedisOperator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseInterceptor {

    @Autowired
    public RedisOperator redis;
    public static final String REDIS_USER_TOKEN = "redis_user_token";
    public static final String REDIS_USER_INFO = "redis_user_info";
    public static final String REDIS_ADMIN_TOKEN = "redis_admin_token";
    public static final String REDIS_ALREADY_READ = "redis_already_read";
    //验证token合规
    public boolean verifyUserIdToken(String id,
                                     String token,
                                     String redisKeyPrefix){
        if(StringUtils.isNotBlank(id) && StringUtils.isNotBlank(token)){
            String redisToken = redis.get(redisKeyPrefix + ":" + id);
            if(StringUtils.isBlank(redisToken)){ //如果redis中token不存在
                GraceException.display(ResponseStatusEnum.UN_LOGIN);
                return false;
            }else{
                if(!redisToken.equalsIgnoreCase(token)){ //如果传入的token和redis里面存储的token不一致
                    GraceException.display(ResponseStatusEnum.TICKET_INVALID);
                    return false;
                }
            }
        }else{
            GraceException.display(ResponseStatusEnum.UN_LOGIN);
            return false;
        }

        return true;
    }

}
